package com.wsoft.form.controller;

import com.github.pagehelper.PageInfo;
import com.wsoft.constant.ExcelConstant;
import com.wsoft.form.query.excel.ExportExcelTemplateQuery;
import com.wsoft.form.query.operation.*;
import com.wsoft.form.service.IWfFormOperationService;
import com.wsoft.response.ResponsePage;
import com.wsoft.response.ResponseResult;
import com.wsoft.core.utils.FormExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author z001
 * @Date 2024/8/30 14:47
 */
@RestController
@Tag(name = "表单操作")
@RequestMapping("/wf/op")
public class WfFormOperationController {

    @Resource
    private IWfFormOperationService operationService;

    @Operation(summary = "新增数据接口",description = "新增数据接口")
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody @Valid OperationInsertQuery query) {
        operationService.addOperation(query);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "更新数据接口",description = "更新数据接口")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Valid OperationUpdateQuery query) {
        operationService.updateOperation(query);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "删除数据接口",description = "更新数据接口")
    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody OperationDelQuery query) {
        operationService.delOperation(query.getDesignId(),query.getDataIdList());
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "获取数据详情",description = "获取数据详情")
    @GetMapping("/get")
    public ResponseResult get(@RequestParam("designId") Long designId, @RequestParam("dataId") Long dataId) {
        Map<String, Object> map = operationService.getOperation(designId,dataId);
        return ResponseResult.success(map);
    }


    @Operation(summary = "查询数据接口",description = "查询数据接口")
    @PostMapping("/list")
    public ResponseResult list(@RequestBody OperationListQuery query) {
        PageInfo<Map<String, Object>> list = operationService.listOperation(query);
        return ResponseResult.success(ResponsePage.restPage(list));
    }


    @Operation(summary = "查询数据关联接口",description = "查询数据关联接口")
    @PostMapping("/linkList")
    public ResponseResult linkList(@RequestBody LinkListQuery query) {
        PageInfo<Map<String, Object>> list = operationService.linkList(query);
        return ResponseResult.success(list);
    }


    @Operation(summary = "导出模板",description = "导出模板")
    @PostMapping("/exportTemplate")
    public void exportTemplate(@RequestBody ExportExcelTemplateQuery query, HttpServletResponse response) throws IOException {
        String designName = operationService.exportTemplate(query.getDesignId());
        //导出模板
        FormExcelUtil.exportFormTemplate(response,designName + ExcelConstant.DEFAULT_EXPORT_TEMPLATE_NAME,
                query.getDesignId(), query.getParentId(),null,null);
    }


    @Operation(summary = "导出数据",description = "导出数据")
    @PostMapping("/exportData")
    public void exportTemplateData(@RequestBody OperationListQuery query, HttpServletResponse response) throws IOException {
        Map<String, List<List<String>>> resultMap = operationService.exportTemplateData(query);
        FormExcelUtil.exportFormData(response,"data.xlsx","sheet",resultMap.get("headList"),resultMap.get("dataList"));
    }

    @Operation(summary = "导出数据-带子表",description = "导出数据-带子表")
    @PostMapping("/exportMultiData")
    public void exportTemplateMultiData(@RequestBody List<OperationListQuery> queryList, HttpServletResponse response) throws IOException {
        List<List<List<String>>> headList = new ArrayList<>();
        List<List<List<String>>> dataList = new ArrayList<>();
        for (OperationListQuery query : queryList) {
            Map<String, List<List<String>>> resultMap = operationService.exportTemplateData(query);
            headList.add(resultMap.get("headList"));
            dataList.add(resultMap.get("dataList"));
        }
        List<String> list = Arrays.asList("sheet1", "sheet2");
        FormExcelUtil.exportFormData(response,"data.xlsx",list,headList,dataList);
    }
}
